package org.zhouhe.tag.report;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zhouhe.modules.report.service.ReportService;
import org.zhouhe.util.Constant;
import org.zhouhe.util.Utils;
import org.zhouhe.vo.Industry;
import org.zhouhe.vo.Report;
import org.zhouhe.vo.ReportType;

@SuppressWarnings("unchecked")
public class ReportWithRecommend extends TagSupport {

	private static final long serialVersionUID = -5129179921556811260L;
	private ServletContext servletContext;
	private WebApplicationContext wac;
	private ReportService reportService;

	private Integer typeId;
	private Integer industryId;
	private Integer industryCount = 10;
	private Integer resultSize = 10;
	private String className;

	public int doStartTag() {
		try {
			String menuClass = Utils.generateGUID();
			String contentClass = Utils.generateGUID();
			StringBuilder html = new StringBuilder();
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			servletContext = request.getSession().getServletContext();
			wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
			List<Industry> industryList = new ArrayList<Industry>();
			List<Industry> allIndustries = (List<Industry>) servletContext.getAttribute(Constant.APPLICATION_KEY_INDUSTRY_LIST);
			if(industryId != null){
				for(Industry ind : allIndustries){
					if(industryId.equals(ind.getIndustryId())){
						industryList.add(ind);
						break;
					}
				}
			}else{
				industryList.addAll(allIndustries);
			}
			reportService = (ReportService) wac.getBean("reportService");
			ReportType reportType = reportService.getReportType(typeId);
			html.append("<div class='"+ className +"'>");
			html.append("  <ul class='blockTag-title-ul'>");
			html.append("    <li class='blockTag-title-text'>"+ reportType.getName() +"</li>");
			List<Industry> industrys = new ArrayList<Industry>();
			String menuClassName = "block-title-hover";
			Map<Integer, List<Report>> map = new HashMap<Integer, List<Report>>();
			for (int i = 0; i < industryList.size() && i < industryCount; i++) {
				Industry ind = new Industry();
				ind.setIndustryId(industryList.get(i).getIndustryId());
				ind.setName(industryList.get(i).getName());
				String menuID = Utils.generateGUID();
				String contentID = Utils.generateGUID();
				ind.setIdPath(contentID);
				industrys.add(ind);
				List<Report> subReportList = reportService.getReportByType(typeId, industryList.get(i).getIndustryId(), resultSize, false);
//				ind.setReportList(subReportList);
				map.put(ind.getIndustryId(), subReportList);
				html.append("<li class='"+ menuClass +" "+ menuID +" "+ menuClassName +"' onMouseOver=\"mouseOverType('"+ menuClass +"', '"+ menuID +"','block-title-normal','block-title-hover', '"+ contentClass +"', '"+ contentID +"')\"><a href='reportBlocks4txi1.action?reportType.reportTypeId="+ typeId +"&paramIndustry.industryId="+ industryList.get(i).getIndustryId() +"'>");
				String displayName = industryList.get(i).getName();
				if(this.industryId == null){
					displayName = displayName.substring(0,2);
				}
				html.append(displayName);
				html.append("</a></li>");
				menuClassName = "block-title-normal";
			}
			html.append("    <li style='float:right'><a target='_blank' href='reportList4txix?reportType.reportTypeId="+ typeId +"'>更多&gt;&gt;</a></li>");
			html.append("  </ul>");
			html.append("");
			html.append("  <div class='blockTag-content-div'>");
			String divDisplay = "block";
			for(Industry ind : industrys){
//				List<Report> subReportList = new ArrayList<Report>(ind.getReportList());
				List<Report> subReportList = map.get(ind.getIndustryId());
				Report recommendedRpt = null;
				if (subReportList.size() > 0) {
					recommendedRpt = subReportList.get(0);
				} else {
					recommendedRpt = new Report();
					recommendedRpt.setTitle("");
					recommendedRpt.setRecommendTips("");
				}
				html.append("    <div style='display:"+ divDisplay +";' class='"+ contentClass +" "+ ind.getIdPath() +"'>");
				divDisplay = "none";
				html.append("      <ul class='blockTag-content-recommend-ul'>");
				html.append("        <li class='blockTag-content-recommend-title'><a target='_blank' href='report.action?report.reportId="+ recommendedRpt.getReportId() +"'>" + recommendedRpt.getTitle() + "</a></li>");
				html.append("        <li class='blockTag-content-recommend-tips'>" + Utils.formatTitle(recommendedRpt.getSummary(), 200) + "</li>");
				html.append("      </ul>");
				html.append("      <ul class='blockTag-content-list-ul'>");
				if (subReportList.size() > 1) {
					subReportList.remove(0);
					for (Report rpt : subReportList) {
						html.append("      <li>• <a target='_blank' href='report.action?report.reportId="+ rpt.getReportId() +"'>" + rpt.getTitle() + "</a></li>");
					}
				}
				html.append("      </ul>");
				html.append("    </div>");
			}
			
			html.append("  </div>");
			html.append("  <div style='clear:both;'></div>");
			html.append("</div>");
			html.append("");
			html.append("");
			html.append("");
			JspWriter out = pageContext.getOut();
			out.print(html.toString());
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public void setTypeId(Integer typeId) {
		this.typeId = typeId;
	}

	public Integer getTypeId() {
		return typeId;
	}

	public void setIndustryCount(Integer industryCount) {
		this.industryCount = industryCount;
	}

	public Integer getIndustryCount() {
		return industryCount;
	}

	public void setResultSize(Integer resultSize) {
		this.resultSize = resultSize;
	}

	public Integer getResultSize() {
		return resultSize;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getClassName() {
		return className;
	}

	public void setIndustryId(Integer industryId) {
		this.industryId = industryId;
	}

	public Integer getIndustryId() {
		return industryId;
	}

}
